package com.we.wecc.aw.mid.impl;

import com.google.common.collect.Lists;
import com.we.wecc.aw.db.mapper.AwardInvestLogMapper;
import com.we.wecc.aw.db.mapper.AwardMapper;
import com.we.wecc.aw.db.model.Award;
import com.we.wecc.aw.db.model.AwardInvestLog;
import com.we.wecc.aw.db.model.AwardInvestLogExample;
import com.we.wecc.aw.mid.service.InvestService;
import com.we.wecc.us.db.mapper.ActUserMapper;
import com.we.wecc.us.db.mapper.UserMapper;
import com.we.wecc.us.db.model.ActUser;
import com.we.wecc.us.db.model.User;
import com.we.wecc.us.db.model.UserTicker;
import com.we.wecc.us.mid.service.ActUserService;
import com.we.wecc.ut.mid.service.TickerService;
import com.x.share.mid.exception.HelperException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class InvestServiceImpl implements InvestService {

    @Resource
    ActUserService actUserService;

    @Resource
    ActUserMapper actUserMapper;

    @Resource
    AwardInvestLogMapper awardInvestLogMapper;

    @Resource
    TickerService tickerService;

    @Resource
    UserMapper userMapper;

    @Resource
    AwardMapper awardMapper;

    @Transactional
    @Override
    public Boolean invest(Long userId, Integer awardId) {
        Boolean flag = false;
        Award aw = awardMapper.selectByPrimaryKey(awardId);
        if (aw == null || aw.getDelFlag().equals(-1)) {
            throw new HelperException("该条活动不存在!");
        }
        AwardInvestLogExample ex = new AwardInvestLogExample();
        ex.or().andUserIdEqualTo(userId).andAwardIdEqualTo(awardId);
        int count = (int) awardInvestLogMapper.countByExample(ex);
        if (count == aw.getTimes()) {
            throw new HelperException("本期活动您的参与次数已达上限！");
        }
        ActUser actUser = actUserService.getActUser(userId);
        if (!actUser.getInvestTimes().equals(0)) { //如果每日的投资次数用完，使用大转盘得到的次数
            actUser.setInvestTimes(actUser.getInvestTimes() - 1);
            actUserMapper.updateByPrimaryKeySelective(actUser);
        } else {
            if (actUser.getInvestRemainTimes().equals(0))
                throw new HelperException("您的剩余次数不足，请明天再来或者大转盘获取更多次数！");
            actUser.setInvestRemainTimes(actUser.getInvestRemainTimes() - 1);
            actUserMapper.updateByPrimaryKeySelective(actUser);
        }
        double x = (double) aw.getInvestment();
        UserTicker ut = tickerService.getUserTicker(userId, "wecc");
        if (ut.getNum() < x) {
            throw new HelperException("您的余额不足，请先去充值！");
        }
        tickerService.addTicker(userId, "wecc", -x, "云购投资");
        AwardInvestLog ail = new AwardInvestLog();
        ail.setAwardId(awardId);
        ail.setNum(x);
        ail.setUserId(userId);
        ail.setCreateTime(new Date());
        int b = awardInvestLogMapper.insert(ail);
        if (b == 1) {
            flag = true;
        }
        return flag;
    }

    @Override
    public List<String> getProclamation() {
        AwardInvestLogExample ex = new AwardInvestLogExample();
        ex.setOrderByClause("createTime desc");
        List<AwardInvestLog> list = awardInvestLogMapper.selectByExample(null);
        List<String> proclamation = Lists.newArrayList();
        list.stream().limit(10).forEach(s -> {
            User user = userMapper.selectByPrimaryKey(s.getUserId());
            String msg = String.format("%s投注了%sWECC", user.getName(), s.getNum());
            proclamation.add(msg);
        });
        return proclamation;
    }
}
